package com.hui.platform.falseworkapi.common.valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * Validator全局配置
 *
 * @author Peng
 * @date 2019/11/18
 */
@Configuration
public class ValidatorGlobalConfig {

    /**
     * 配置国际化与校验器关联
     * @return 校验器
     */
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @Autowired
    public LocalValidatorFactoryBean localValidatorFactoryBean(MessageSource messageSource) {
        //自动原配置
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
        factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
        //配置国际化
        factoryBean.setValidationMessageSource(messageSource);
        return factoryBean;
    }

}


